@@ -2,8 +2,6 @@ module Agents |
||
2 | 2 |
class PostAgent < Agent |
3 | 3 |
include WebRequestConcern |
4 | 4 |
|
5 |
- cannot_create_events! |
|
6 |
- |
|
7 | 5 |
default_schedule "never" |
8 | 6 |
|
9 | 7 |
description <<-MD |
@@ -15,6 +13,9 @@ module Agents |
||
15 | 13 |
|
16 | 14 |
By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`). Change `content_type` to `json` to send JSON instead. Change `content_type` to `xml` to send XML, where the name of the root element may be specified using `xml_root`, defaulting to `post`. |
17 | 15 |
|
16 |
+ If `emit_events` is set to `true`, the server response will be emitted as an Event and can be fed to a WebsiteAgent for parsing (using its `data_from_event` and `type` options). No data processing |
|
17 |
+ will be attempted by this Agent, so the "body" value will always be raw text. |
|
18 |
+ |
|
18 | 19 |
Other Options: |
19 | 20 |
|
20 | 21 |
* `headers` - When present, it should be a hash of headers to send with the request. |
@@ -23,7 +24,17 @@ module Agents |
||
23 | 24 |
* `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}"). |
24 | 25 |
MD |
25 | 26 |
|
26 |
- event_description "Does not produce events." |
|
27 |
+ event_description <<-MD |
|
28 |
+ Events look like this: |
|
29 |
+ { |
|
30 |
+ "status": 200, |
|
31 |
+ "response_headers": { |
|
32 |
+ "Content-Type": "text/html", |
|
33 |
+ ... |
|
34 |
+ }, |
|
35 |
+ "body": "<html>Some data...</html>" |
|
36 |
+ } |
|
37 |
+ MD |
|
27 | 38 |
|
28 | 39 |
def default_options |
29 | 40 |
{ |
@@ -56,6 +67,10 @@ module Agents |
||
56 | 67 |
errors.add(:base, "if provided, payload must be a hash") |
57 | 68 |
end |
58 | 69 |
|
70 |
+ if options['emit_events'].present? && ![true, false, 'true', 'false'].include?(options['emit_events']) |
|
71 |
+ errors.add(:base, "if provided, emit_events must be true or false") |
|
72 |
+ end |
|
73 |
+ |
|
59 | 74 |
unless %w[post get put delete patch].include?(method) |
60 | 75 |
errors.add(:base, "method must be 'post', 'get', 'put', 'delete', or 'patch'") |
61 | 76 |
end |
@@ -112,9 +127,13 @@ module Agents |
||
112 | 127 |
error "Invalid method '#{method}'" |
113 | 128 |
end |
114 | 129 |
|
115 |
- faraday.run_request(method.to_sym, url, body, headers) { |request| |
|
130 |
+ response = faraday.run_request(method.to_sym, url, body, headers) { |request| |
|
116 | 131 |
request.params.update(params) if params |
117 | 132 |
} |
133 |
+ |
|
134 |
+ if boolify(interpolated['emit_events']) |
|
135 |
+ create_event payload: { body: response.body, response_headers: response.headers, status: response.status } |
|
136 |
+ end |
|
118 | 137 |
end |
119 | 138 |
end |
120 | 139 |
end |
@@ -59,6 +59,8 @@ module Agents |
||
59 | 59 |
"description": { "path": "results.data[*].description" } |
60 | 60 |
} |
61 | 61 |
|
62 |
+ The `extract` option can be skipped for the JSON type, causing the full JSON response to be returned. |
|
63 |
+ |
|
62 | 64 |
# Scraping Text |
63 | 65 |
|
64 | 66 |
When parsing text, each sub-hash should contain a `regexp` and `index`. Output text is matched against the regular expression repeatedly from the beginning through to the end, collecting a captured group specified by `index` in each match. Each index should be either an integer or a string name which corresponds to <code>(?<<em>name</em>>...)</code>. For example, to parse lines of <code><em>word</em>: <em>definition</em></code>, the following should work: |
@@ -50,7 +50,7 @@ describe Agents::PostAgent do |
||
50 | 50 |
raise "unexpected Content-Type: #{content_type}" |
51 | 51 |
end |
52 | 52 |
end |
53 |
- { status: 200, body: "ok" } |
|
53 |
+ { status: 200, body: "<html>a webpage!</html>", headers: { 'Content-Type' => 'text/html' } } |
|
54 | 54 |
} |
55 | 55 |
end |
56 | 56 |
|
@@ -186,6 +186,40 @@ describe Agents::PostAgent do |
||
186 | 186 |
|
187 | 187 |
expect(@sent_requests[:get][0].data).to eq(@checker.options['payload'].to_query) |
188 | 188 |
end |
189 |
+ |
|
190 |
+ describe "emitting events" do |
|
191 |
+ context "when emit_events is not set to true" do |
|
192 |
+ it "does not emit events" do |
|
193 |
+ expect { |
|
194 |
+ @checker.check |
|
195 |
+ }.not_to change { @checker.events.count } |
|
196 |
+ end |
|
197 |
+ end |
|
198 |
+ |
|
199 |
+ context "when emit_events is set to true" do |
|
200 |
+ before do |
|
201 |
+ @checker.options['emit_events'] = 'true' |
|
202 |
+ @checker.save! |
|
203 |
+ end |
|
204 |
+ |
|
205 |
+ it "emits the response status" do |
|
206 |
+ expect { |
|
207 |
+ @checker.check |
|
208 |
+ }.to change { @checker.events.count }.by(1) |
|
209 |
+ expect(@checker.events.last.payload['status']).to eq 200 |
|
210 |
+ end |
|
211 |
+ |
|
212 |
+ it "emits the body" do |
|
213 |
+ @checker.check |
|
214 |
+ expect(@checker.events.last.payload['body']).to eq '<html>a webpage!</html>' |
|
215 |
+ end |
|
216 |
+ |
|
217 |
+ it "emits the response headers" do |
|
218 |
+ @checker.check |
|
219 |
+ expect(@checker.events.last.payload['response_headers']).to eq({ 'Content-Type' => 'text/html' }) |
|
220 |
+ end |
|
221 |
+ end |
|
222 |
+ end |
|
189 | 223 |
end |
190 | 224 |
|
191 | 225 |
describe "#working?" do |
@@ -286,5 +320,22 @@ describe Agents::PostAgent do |
||
286 | 320 |
@checker.options['headers'] = { "Authorization" => "foo bar" } |
287 | 321 |
expect(@checker).to be_valid |
288 | 322 |
end |
323 |
+ |
|
324 |
+ it "requires emit_events to be true or false, if present" do |
|
325 |
+ @checker.options['emit_events'] = 'what?' |
|
326 |
+ expect(@checker).not_to be_valid |
|
327 |
+ |
|
328 |
+ @checker.options['emit_events'] = '' |
|
329 |
+ expect(@checker).to be_valid |
|
330 |
+ |
|
331 |
+ @checker.options['emit_events'] = 'true' |
|
332 |
+ expect(@checker).to be_valid |
|
333 |
+ |
|
334 |
+ @checker.options['emit_events'] = 'false' |
|
335 |
+ expect(@checker).to be_valid |
|
336 |
+ |
|
337 |
+ @checker.options['emit_events'] = true |
|
338 |
+ expect(@checker).to be_valid |
|
339 |
+ end |
|
289 | 340 |
end |
290 | 341 |
end |